/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.xue.admin.system.mybatis.entity.SystemDepartment;
import com.wyl.xue.admin.system.service.Department;
import com.wyl.xue.admin.system.vo.DepartmentInfoVo;
import com.wyl.xue.admin.system.vo.DepartmentTree;
import com.wyl.xue.core.util.result.WebResponse;
import com.wyl.xue.core.util.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName: DepartmentController
 * @Function: 完成部门所需接口开发
 * @Date: 2019/12/23 22:12
 * @author wyl
 * @version V1.1
 */
@RequestMapping("/admin/v1/department")
@Api(tags = {"部门信息接口"})
@AllArgsConstructor
@RestController
@CrossOrigin
public class DepartmentController {

    final private Department department;

    @PostMapping(value = "/")
    @PreAuthorize("hasAuthority('sys:dept:add')")
    @ApiOperation(value = "添加部门接口", notes = "添加部门接口")
    public WebResult<Boolean> addDepartment(@Valid @RequestBody DepartmentInfoVo departmentInfoVo) {
        department.addDepartment(departmentInfoVo);
        return WebResponse.WebResponse.ok();
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改部门信息接口", notes = "修改部门信息接口")
    @PreAuthorize("hasAuthority('sys:dept:update')")
    public WebResult<Boolean> changeDepartment(@Valid @RequestBody SystemDepartment systemDepartment) {
        department.updateDepartment(systemDepartment);
        return WebResponse.WebResponse.ok();
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除部门信息", notes = "根据部门ID删除指定部门")
    @PreAuthorize("hasAuthority('sys:dept:del')")
    public WebResult<Boolean> deleteDepartment(@PathVariable long id) {
        department.deleteDepartment(id);
        return WebResponse.WebResponse.ok();
    }

    @ApiOperation(value = "获取部门树", notes = "获取部门树")
    @GetMapping(value = "/tree")
    @PreAuthorize("hasAuthority('sys:dept:get')")
    public WebResult<List<DepartmentTree>> getDepartmentTree() {
        return WebResponse.WebResponse.ok(department.getDepartmentTree(-1L));
    }

    @ApiOperation(value = "获取指定部门下的一级子目录")
    @GetMapping(value = "/{id}/{page}/{size}")
    @PreAuthorize("hasAuthority('sys:dept:get')")
    public WebResult<IPage<SystemDepartment>> getSubdirectoryById(@PathVariable long id, @PathVariable int page, @PathVariable int size) {
        return WebResponse.WebResponse.ok(department.getSubdirectoryDepartmentByParentIdForPage(id, page, size));
    }

    @PostMapping(value = "/search")
    @ApiOperation(value = "获取菜单树 不包含按钮")
    @PreAuthorize("hasAuthority('sys:dept:search')")
    public WebResult<List<SystemDepartment>> search(@RequestBody DepartmentInfoVo departmentInfoVo) {
        return WebResponse.WebResponse.ok(department.searchDepartment(departmentInfoVo));
    }
}
